namespace :document do
  desc "Generate documentation for the application. Set custom template with TEMPLATE=/path/to/rdoc/template.rb"
  task :app do
    Rake::Task['document:regen_app'].invoke
    Rake::Task['document:relocate'].invoke
  end
  
  task :models do
    schema_info = /(^# == Schema Information.*?\n(#.*\n)*\n)/
    model_dir = File.join(RAILS_ROOT, "app/models")
    models = nil

    Dir.chdir(model_dir) do 
      models = Dir["**/*.rb"].map { |file_name| File.join(model_dir, file_name) }
    end

    models.each do |file_name|
      if File.exist?(file_name)
        content = File.read(file_name)
        if content =~ schema_info
          puts $1
        end
      end
    end
  end
  
  task :plugins do
    Rake::Task['document:regen_plugins'].invoke
  end
  
  desc "Replace bad frameset src attributes with proper site root"
  task :relocate do
    sh %(find ./public -name '*.html' -print0 | xargs -0 perl -pi -e 's/src="(?!\\docs\\/)/src="\\/docs\\//g')    
    sh %(perl -pi -e 's/url=files\\//url=\\/docs\\/files\\//g' ./public/docs/index.html) 
  end
  
  Rake::RDocTask.new("gen_app") do |rdoc|
    rdoc.rdoc_dir = 'public/docs'
    rdoc.template = ENV['template'] if ENV['template']
    rdoc.title    = "Rails Application Documentation"
    rdoc.options << '--line-numbers' << '--inline-source'
    rdoc.options << '--charset' << 'utf-8'
    if File.exists?("public/docs/README_FOR_APP")
      rdoc.rdoc_files.include('public/docs/README_FOR_APP')
      rdoc.main "public/docs/README_FOR_APP"
    end
    rdoc.rdoc_files.include('app/**/*.rb')
    rdoc.rdoc_files.include('lib/**/*.rb')
  end
  
  Rake::RDocTask.new("gen_plugins") do |rdoc|
    rdoc.rdoc_dir = 'public/docs/plugins'
    rdoc.template = ENV['template'] if ENV['template']
    rdoc.title    = "Rails Plugin Documentation"
    rdoc.options << '--line-numbers' << '--inline-source'
    rdoc.options << '--charset' << 'utf-8'
    if File.exists?("public/docs/README_FOR_APP")
      rdoc.rdoc_files.include('public/docs/README_FOR_APP')
      rdoc.main "public/docs/README_FOR_APP"
    end
    rdoc.rdoc_files.include('app/**/*.rb')
    rdoc.rdoc_files.include('lib/**/*.rb')
  end

  plugins = FileList['vendor/plugins/**'].collect { |plugin| File.basename(plugin) }

  desc "Generate documentation for all installed plugins"
  task :gen_plugins => plugins.collect { |plugin| "document:plugins:#{plugin}" }

  desc "Remove plugin documentation"
  task :clobber_plugins do 
    rm_rf 'doc/plugins' rescue nil
  end

  namespace :plugins do
    # Define doc tasks for each plugin
    plugins.each do |plugin|
      task(plugin => :environment) do
        plugin_base   = "vendor/plugins/#{plugin}"
        options       = []
        files         = Rake::FileList.new
        options << "-o public/docs/plugins/#{plugin}"
        options << "--title '#{plugin.titlecase} Plugin Documentation'"
        options << '--line-numbers' << '--inline-source'
        options << '-T html'

        files.include("#{plugin_base}/lib/**/*.rb")
        if File.exists?("#{plugin_base}/README")
          files.include("#{plugin_base}/README")    
          options << "--main '#{plugin_base}/README'"
        end
        files.include("#{plugin_base}/CHANGELOG") if File.exists?("#{plugin_base}/CHANGELOG")

        options << files.to_s

        sh %(rdoc #{options * ' '})
      end
    end
  end
end